package drools;

import org.drools.io.ResourceFactory;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;

public class DroolsUtils {

    private static KieContainer kContainer ;
    private static KieBase kieBase ;

    static {
        KieServices kieServices = KieServices.Factory.get();
        // 根据 kmodule.xml加载drl 只会加载 test-resources下的
        //kContainer = kieServices.getKieClasspathContainer();
        //kieBase = kContainer.getKieBase("KBase_biz_test");

        // 手动编译，加载 src-resources下的drl
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write(ResourceFactory.newClassPathResource("rules/case_indiv_info.drl"));
        kieFileSystem.write(ResourceFactory.newClassPathResource("rules/case_info.drl"));
        kieFileSystem.write(ResourceFactory.newClassPathResource("rules/case_mort.drl"));
        kieFileSystem.write(ResourceFactory.newClassPathResource("rules/credit.drl"));
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();
        kContainer = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId());
        kieBase = kContainer.getKieBase();

    }

    public static KieSession getKieSession() {
        return kieBase.newKieSession();
    }


    public static StatelessKieSession getStatelessKieSession() {
        return kieBase.newStatelessKieSession();
    }
}
